<template>
  <el-dialog v-el-drag-dialog title="导入表" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-card style="margin:0 0 5px 5px">
      <el-form ref="queryForm" :inline="true" :model="queryData">
        <el-form-item prop="dbKey">
          <el-select v-model="queryData.dbKey" filterable reserve-keyword clearable>
            <el-option
              v-for="item in optionOfDbSource"
              :key="item.id"
              :label="item.text"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数据表名称" prop="tableName">
          <el-input v-model="queryData.tableName" clearable  @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item m-l-20 m-r-20" type="primary" round icon="el-icon-search" @click="handleFilter">
            搜索
          </el-button>
          <el-button v-permission="['tool:gen:init']" v-if="selectionData.length > 0" type="success" round icon="el-icon-check" @click="handleSave">
            导入
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="m-l-5">
      <el-row>
        <el-table
          ref="queryTable"
          v-loading="tableLoading"
          row-key="tableName"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="tableName" label="表名称" :show-overflow-tooltip="true" />
          <el-table-column prop="tableComment" label="表描述" :show-overflow-tooltip="true" />
          <el-table-column prop="className" label="类名称" :show-overflow-tooltip="true" />
        </el-table>
      </el-row>
    </el-card>
    <span slot="footer" class="dialog-footer">
      <el-button type="info" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { generatorInit } from '@/api/tool/generator'
import { dbaseSelectData, dbaseTableData } from '@/api/tool/dbase'

export default {
  name: 'GenTableImport',
  props: {
    appendToBody: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      visible: false,
      hasLoaded: false,
      queryData: {
        dbKey: undefined,
        tableName: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      selectionData: [],
      optionOfDbSource: []
    }
  },
  methods: {
    init() {
      this.visible = true
      this.hasLoaded = false
      this.getDbData()
    },
    getDbData() {
      dbaseSelectData().then(response => {
        this.optionOfDbSource = [...this.optionOfDbSource, ...response.param.data]
      }).catch(e => {})
    },
    handleFilter() {
      if (!this.queryData.dbKey) {
        this.$modal.msgWarning('请先选择数据源!')
        return
      }

      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.tableData = []
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      dbaseTableData(this.queryData.dbKey, query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
      })
    },
    handleSelectionChange(selection) {
      this.selectionData = selection.map(item => item.tableName);
    },
    handleSave() {
      const tableNames = this.selectionData ? this.selectionData.join(',') : ''
      generatorInit({
        dbKey: this.queryData.dbKey,
        tableNames: tableNames
      }).then(response => {
        this.$modal.msgSuccess(response.msg)
        this.$emit('confirm')
        this.onClose()
      }).catch(() => {})
    },
    onClose() {
      this.$refs.queryForm.resetFields()
      this.tableData = []
      this.optionOfDbSource = []
      this.visible = false
    }
  }
}
</script>
